﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using CommandPrompt;

namespace Bacon_FTP_Client
{
    /// <summary>
    /// Class that contains the services for comunication and transmision for the server
    /// </summary>
    public class ClientServices
    {
        TcpClient server = new TcpClient();
        //private Thread listeningThread;
        //private Thread WritingThread;

        //public ClientServices() { }

        /// <summary>
        /// Client services for writing and listening interactions.
        /// </summary>
        /// <param name="client">Client instance for the services initialization</param>
        #region ClientServices()
        public ClientServices(Client client)
        {
            IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(client._hostname.ToString()), client._port);

            try
            {  
                server.Connect(serverEndPoint);
                NetworkStream serverStream = server.GetStream();

                //Start Listening and Writing Threads
                //this.listeningThread = new Thread(new ThreadStart(ListenForResponses));
                //this.WritingThread = new Thread(new ThreadStart(WriteCommands));
                new Thread(new ParameterizedThreadStart(ListenForResponses)).Start(serverStream);
                //new Thread(new ParameterizedThreadStart(WriteCommands)).Start(serverStream);
                //this.listeningThread.Start();
                //this.WritingThread.Start();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("El cliente no pudo conectarse. Verifique la direccion destino...\n" + ex.Message);
                
                //throw;
            }
        }
        #endregion

        /// <summary>
        /// Client listener thread for server responses.
        /// </summary>
        /// <param name="Sender">Object Sender</param>
        #region ListenForResponses()
        private void ListenForResponses(object Sender)
        {            
            try
            {
                NetworkStream serverStream = (NetworkStream)Sender;

                ASCIIEncoding encoder = new ASCIIEncoding();
                byte[] message = new byte[1024];
                int bytesRead;

                while (true)
                {
                    bytesRead = serverStream.Read(message, 0, 1024);

                    if (bytesRead == 0)
                        break;
                             
                    //Console.Write(encoder.GetString(message, 0, bytesRead));
                }
            }
            catch (Exception)
            {               
                
                //throw;
            }
        }
        #endregion

        /// <summary>
        /// Client writer thread to send commands to the server.
        /// </summary>
        /// <param name="Sender">Object Sender</param>
        #region WriteCommands()
        //public void WriteCommands(object Sender)
        //{
        //    try
        //    {
        //        NetworkStream serverStream = (NetworkStream)Sender;

        //        ASCIIEncoding encoder = new ASCIIEncoding();
        //        byte[] buffer; // = encoder.GetBytes("Hello Server! Client BACON CLIENT CONECTED...");
        //        //serverStream.Write(buffer, 0, buffer.Length);
        //        //Limpio el buffer
        //        //buffer = null;

        //        while (true)
        //        {
        //            buffer = encoder.GetBytes();

        //            serverStream.Write(buffer, 0, buffer.Length);
        //            serverStream.Flush();

        //            if ( == "exit")
        //                break;

        //            //Limpio el buffer
        //            //buffer = null;
        //        }
        //    }
        //    catch (Exception)
        //    {
        //        System.Windows.Forms.MessageBox.Show("Falla de escritura");
        //        //throw;
        //    }
        //}
        #endregion
    }
}
